﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Common
{
  public  class RegexHelper
    {
      /// <summary>
      /// 正则验证
      /// </summary>
      /// <param name="src">要匹配的源字符</param>
      /// <param name="pattern">正则表达式</param>
      /// <returns>是否匹配</returns>
      public static bool IsMatch(string src,string pattern)
      {
          if (string.IsNullOrEmpty(src) || string.IsNullOrEmpty(pattern))
          {
              throw new ArgumentException("参数不能为空");
          }
          return Regex.IsMatch(src,pattern);
      }
      /// <summary>
      /// 正则验证
      /// </summary>
      /// <param name="src">要匹配的源字符</param>
      /// <param name="pattern">正则表达式</param>
      ///<param name="options">条件</param>
      /// <returns>是否匹配</returns>
      public static bool IsMatch(string src, string pattern, RegexOptions options)
      {
          if (string.IsNullOrEmpty(src) || string.IsNullOrEmpty(pattern))
          {
              throw new ArgumentException("参数不能为空");
          }
          return Regex.IsMatch(src, pattern,options);
      }
      /// <summary>
      /// 正则替换
      /// </summary>
      /// <param name="src">原字符串</param>
      /// <param name="pattern">正则表达式</param>
      /// <param name="replacement">替换成什么</param>
      /// <returns>替换之后的字符串</returns>
      public static string Replace(string src, string pattern, string replacement)
      {
         return Regex.Replace(src, pattern, replacement);
      }
      /// <summary>
      /// 正则替换
      /// </summary>
      /// <param name="src">原字符串</param>
      /// <param name="pattern">正则表达式</param>
      /// <param name="replacement">替换成什么</param>
      /// <param name="count">替换次数</param>
      /// <param name="startat">替换的起始位置</param>
      /// <returns></returns>
      public static string Replace(string src, string pattern, string replacement, int count,int startat)
      {
          Regex regex = new Regex(pattern);
          return regex.Replace(src,replacement,count,startat);
      }
    }
}
